﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Activity.Data;
using Nancy.Extensions;
using Newtonsoft.Json;

namespace Activity.API
{
    public class CalendarHandler
    {
        private const string COMPANYID = "KM";
        public IList<CalendarModel.ListModel> GetFestivalList(int Year = -1)
        {
            try
            {
                if (Year == -1)
                {
                    Year = DateTime.Now.Year;
                }
                var db = new ActivityEntities();
                var query = (from hd in db.KM_YearHoliday
                    where hd.HolidayYearv == Year
                    group hd by new {hd.HolidayYearv, hd.TypeName, hd.IsAllMonth}
                    into temp
                    select new
                    {
                        HolidayYearv = temp.Key.HolidayYearv,
                        TypeName = temp.Key.TypeName,
                        StartDate = temp.Min(d => d.GregorianDay),
                        EndDate = temp.Max(d => d.GregorianDay),
                        IsAllMonth = temp.Key.IsAllMonth
                    }).ToList().Select(d => new CalendarModel.ListModel
                {
                    HolidayYearv = d.HolidayYearv,
                    TypeName = d.TypeName,
                    StartDate = d.StartDate.ToString("yyyy-MM-dd"),
                    EndDate = d.EndDate.ToString("yyyy-MM-dd"),
                    IsAllMonth = d.IsAllMonth
                });
                return query.ToList();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public bool ModifyCalendar(string Json)
        {
            var param = JsonConvert.DeserializeObject<IList<CalendarModel.ListModel>>(Json);
            if (param == null)
            {
                Log.Error("ModifyCalendar：参数对象为空");
                return false;
            }
            var db = new ActivityEntities();
            using (var tran = db.Database.BeginTransaction())
            {

                try
                {
                    var allYear = param.Select(d => new
                    {
                        StartYear = Convert.ToDateTime(d.StartDate).Year,
                        EndYear = Convert.ToDateTime(d.EndDate).Year,
                    }).DistinctBy(d => new { d.StartYear, d.EndYear });
                    var yearList = new List<int>();
                    foreach (var item in allYear)
                    {
                        yearList.Add(item.StartYear);
                        yearList.Add(item.EndYear);
                    }
                    var et = DateTime.Now;
                    var st = DateTime.Now;
                    db.KM_YearHoliday.RemoveRange(db.KM_YearHoliday.Where(d => yearList.Contains(d.HolidayYearv)));
                    foreach (var item in param)
                    {
                        if (string.IsNullOrEmpty(item.StartDate) && string.IsNullOrEmpty(item.EndDate)) continue;
                        et = DateTime.Parse(item.EndDate);
                        st = DateTime.Parse(item.StartDate);
                        for (var i = 0; i <= et.Subtract(st).Days; i++)
                        {
                            var holiday = new KM_YearHoliday
                            {
                                HolidayYearv = st.AddDays(i).Year,
                                TypeName = item.TypeName,
                                GregorianDay = st.AddDays(i),
                                IsAllMonth = item.IsAllMonth,
                                CreateTime = DateTime.Now,
                                CompanyID = COMPANYID
                            };
                            db.KM_YearHoliday.Add(holiday);
                        }
                    }
                    db.SaveChanges();
                    tran.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    Log.Error(ex);
                    return false;
                }
            }
        }

        public class CalendarModel
        {
            public class NeceModel
            {
                public long? HolidayYearv { get; set; }
                public string TypeName { get; set; }
            }

            public class ListModel : NeceModel
            {
                public string StartDate { get; set; }
                public string EndDate { get; set; }
                public bool IsAllMonth { get; set; }
            }
        }
    }
}
